package com.gm.wj.controller;

import com.gm.wj.entity.AdminRole;
import com.gm.wj.result.Result;
import com.gm.wj.result.ResultFactory;
import com.gm.wj.service.AdminPermissionService;
import com.gm.wj.service.AdminRoleMenuService;
import com.gm.wj.service.AdminRolePermissionService;
import com.gm.wj.service.AdminRoleService;
import com.gm.wj.util.SysLoging;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Role controller.
 *
 * @author Evan
 * @date 2019/11
 */
@Api(tags = "角色管理相关接口")
@RestController
public class RoleController {
    @Autowired
    AdminRoleService adminRoleService;
    @Autowired
    AdminPermissionService adminPermissionService;
    @Autowired
    AdminRolePermissionService adminRolePermissionService;
    @Autowired
    AdminRoleMenuService adminRoleMenuService;

    @GetMapping("/api/admin/role")
    @SysLoging("加载角色列表信息") //根据SysLoging的注解信息，进行方法的注释
    public Result listRoles() {
        return ResultFactory.buildSuccessResult("role列表查询成功",adminRoleService.listWithPermsAndMenus(0));
    }

    @GetMapping("/api/admin/roleByUser")
    @SysLoging("修改用户的角色信息") //根据SysLoging的注解信息，进行方法的注释
    public Result listRolesByUser() {
        return ResultFactory.buildSuccessResult("role角色修改查询成功",adminRoleService.listWithPermsAndMenus(1));
    }

    // 根据条件查询
    @GetMapping("/api/admin/role/findByQueryKey")
    @SysLoging("查询角色列表信息") //根据SysLoging的注解信息，进行方法的注释
    public Result findByQueryKey(String queryKey1,String queryKey2 ) {
        return ResultFactory.buildSuccessResult("查询成功",adminRoleService.findByQueryKey(queryKey1,queryKey2));
    }

    @PutMapping("/api/admin/role/status")
    @SysLoging("更新角色状态信息") //根据SysLoging的注解信息，进行方法的注释
    public Result updateRoleStatus(@RequestBody AdminRole requestRole) {
        AdminRole adminRole = adminRoleService.updateRoleStatus(requestRole);
        String message = "用户" + adminRole.getNameZh() + "状态更新成功";
        return ResultFactory.buildSuccessResult(message,null);
    }

    // 修改
    @PutMapping("/api/admin/role")
    @SysLoging("修改角色列表信息") //根据SysLoging的注解信息，进行方法的注释
    public Result editRole(@RequestBody AdminRole requestRole) {
        adminRoleService.editRole(requestRole);
        String message = "修改角色信息成功";
        return ResultFactory.buildSuccessResult(message,null);
    }

    // 添加
    @PostMapping("/api/admin/role")
    @SysLoging("添加角色列表信息") //根据SysLoging的注解信息，进行方法的注释
    public Result addRole(@RequestBody AdminRole requestRole) {
        adminRoleService.addOrUpdate(requestRole);
        return ResultFactory.buildSuccessResult("添加成功",null);
    }

    @GetMapping("/api/admin/role/perm")
    @SysLoging("加载角色列表信息") //根据SysLoging的注解信息，进行方法的注释
    public Result listPerms() {
        return ResultFactory.buildSuccessResult("查询成功",adminPermissionService.list());
    }

    @PutMapping("/api/admin/role/menu")
    @SysLoging("更新角色列表信息") //根据SysLoging的注解信息，进行方法的注释
    public Result updateRoleMenu(@RequestParam int rid, @RequestBody Map<String, List<Integer>> menusIds) {
        adminRoleMenuService.updateRoleMenu(rid, menusIds);
        return ResultFactory.buildSuccessResult("更新成功",null);
    }

    // 删除
    @PostMapping("/api/admin/role/del")
    @SysLoging("删除角色列表信息") //根据SysLoging的注解信息，进行方法的注释
    public Result deleteRole( @RequestParam int id) {
        adminRoleService.deleteById(id);
        return ResultFactory.buildSuccessResult("删除成功",null);
    }

}
